package com.sap.jcp.sm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sap.jcp.sm.domain.Config;
import com.sap.jcp.sm.domain.Shift;
import com.sap.jcp.sm.domain.Track;
import com.sap.jcp.sm.domain.User;
import com.sap.jcp.sm.domain.UserTimeline;
import com.sap.jcp.sm.dto.UserTimelineDTO;
import com.sap.jcp.sm.dto.UserTimelineGridDTO;
import com.sap.jcp.sm.dto.UserTimelineRowDTO;

public class TimelineUtil {

	public UserTimelineDTO createUserTimelineDTO (Map<String, UserTimeline> userTimelineMap, Map<String, User> userMap, 
			String weekId, UserTimelineDTO  userTimelineDTO, Map<String, Shift> shift, Map<String, Track> track,Config config) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		try {
			Date weekStartDate = sdf.parse(weekId);
			Calendar calendar = Calendar.getInstance();
			if (userMap != null) {
				Set<String> userKeySet = userMap.keySet(); 
				Iterator<String> userIterator = userKeySet.iterator();
				List<UserTimelineRowDTO> userTimelineRowList = new ArrayList<UserTimelineRowDTO>();
				while (userIterator.hasNext()) {
					calendar.setTime(weekStartDate);
					User user = userMap.get(userIterator.next());
					String userId = user.getId().toString();
					UserTimelineRowDTO userTimelineRowDTO = new UserTimelineRowDTO();
					userTimelineRowDTO.setUserId(user.getId().toString());
					userTimelineRowDTO.setUserName(user.getName());
					userTimelineRowDTO.setPrimaryTrack(user.getPrimaryTrack());
					List<UserTimelineGridDTO> userTimelineGridList = new ArrayList<UserTimelineGridDTO>();
					for (int i=0; i < 7 ; i++) {
						Date day = calendar.getTime();
						UserTimelineGridDTO userTimelineGridDTO = new UserTimelineGridDTO();
						UserTimeline userTimelineItem = userTimelineMap.get(userId + "_" + DateUtil.getFormattedStringFromDate(day));
						if (userTimelineItem != null) {
							userTimelineGridDTO.setTimeLineId(userTimelineItem.getUserTimelineId());
							userTimelineGridDTO.setShiftId(userTimelineItem.getShift());
							userTimelineGridDTO.setShiftName(shift.get(userTimelineItem.getShift()).getShiftName());
							userTimelineGridDTO.setTrackId(userTimelineItem.getTrack());
							userTimelineGridDTO.setTrackName(track.get(userTimelineItem.getTrack()).getName());
							userTimelineGridDTO.setUserId(userTimelineRowDTO.getUserId());
							userTimelineGridDTO.setUserName(userTimelineRowDTO.getUserName());
						} else {
							userTimelineGridDTO.setShiftId(config.getDefaultShift());
							if (shift.get(config.getDefaultShift()) != null) {
								userTimelineGridDTO.setShiftName(shift.get(config.getDefaultShift()).getShiftName());
							}
							userTimelineGridDTO.setTrackId(user.getPrimaryTrack());
							if (track.get(user.getPrimaryTrack()) != null) {
								userTimelineGridDTO.setTrackName(track.get(user.getPrimaryTrack()).getName());
							} else {
								userTimelineGridDTO.setTrackName("no track");
							}
							userTimelineGridDTO.setUserId(user.getId().toString());
							userTimelineGridDTO.setUserName(user.getName());
						}
						userTimelineGridList.add(userTimelineGridDTO);
						calendar.add(Calendar.DATE, 1);
					}
					userTimelineRowDTO.setUserTimelineGridList(userTimelineGridList);
					userTimelineRowList.add(userTimelineRowDTO);
				}
				userTimelineDTO.setUserTimelineRowList(userTimelineRowList);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return userTimelineDTO;
	}
}
